package com.hepinghospital.user.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.base.BaseFragment;
import com.hepinghospital.user.ui.activity.prescription.MyMedicineActivity;
import com.hepinghospital.user.ui.activity.userinfo.MyFamilyActivity;
import com.hepinghospital.user.ui.activity.userinfo.SettingActivity;
import com.hepinghospital.user.ui.activity.userinfo.UserInfoActivity;
import com.hepinghospital.user.ui.kotlin.DailyRecordListActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.OrderNumbersResponse;
import com.rest.response.UserResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.UIConstants;
import jz.joyoung.robot.util.UtilString;

/**
 * Created by Administrator on 2018/5/25.
 */

@Deprecated
public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.img_head)
    ImageView img_head;

    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;


    @BindView(R.id.tv_unreadCount_dzf)
    TextView tvUnreadCountDzf;
    @BindView(R.id.tv_unreadCount_dfh)
    TextView tvUnreadCountDfh;
    @BindView(R.id.tv_unreadCount_dsh)
    TextView tvUnreadCountDsh;
    @BindView(R.id.tv_unreadCount_ysh)
    TextView tvUnreadCountYsh;
    @BindView(R.id.top)
    RelativeLayout top;
    @BindView(R.id.btn_daizhfu2)
    LinearLayout btnDaizhfu2;
    @BindView(R.id.btn_daifahuo)
    LinearLayout btnDaifahuo;
    @BindView(R.id.btn_yifahuo)
    LinearLayout btnYifahuo;
    @BindView(R.id.btn_yishouhuo)
    LinearLayout btnYishouhuo;
    @BindView(R.id.my_medicine)
    LinearLayout myMedicine;
    @BindView(R.id.btn_daily_record)
    RelativeLayout btnDailyRecord;
    @BindView(R.id.btn_family)
    RelativeLayout btnFamily;
    @BindView(R.id.rl_setting)
    RelativeLayout rlSetting;

    @SuppressLint("StaticFieldLeak")
    private static MineFragment mineFragment;

    public static MineFragment newInstance() {
        if (mineFragment == null) {
            mineFragment = new MineFragment();
        }
        return mineFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {

        //ImageLoaderHelper.getInstance().GlideImageLoader( activity, BaseApplication.avatar, img_head, R.mipmap.img_default );

        mViewPager.setAdapter(new FragmentAdapter(mContext, getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.setCurrentItem(0);
        Objects.requireNonNull(mTabLayout.getTabAt(0)).select();
    }

    @Override
    protected void initData() {
//        getUserInfo();
    }

    //    @OnClick(R.id.btn_daizhifu)
//    void btn_daizhifu() {
//        Bundle bundle = new Bundle();
//        bundle.putInt( "page", 1 );
//        startActivity( MyAskActivity.class, bundle );
//    }
//
//    @OnClick(R.id.btn_weikaishi)
//    void btn_weikaishi() {
//        Bundle bundle = new Bundle();
//        bundle.putInt( "page", 2 );
//        startActivity( MyAskActivity.class, bundle );
//    }
//
//    @OnClick(R.id.btn_jinxingzhong)
//    void btn_jinxingzhong() {
//        Bundle bundle = new Bundle();
//        bundle.putInt( "page", 3 );
//        startActivity( MyAskActivity.class, bundle );
//    }
//
//    @OnClick(R.id.btn_daipingjia)
//    void btn_daipingjia() {
//        Bundle bundle = new Bundle();
//        bundle.putInt( "page", 4 );
//        startActivity( MyAskActivity.class, bundle );
//    }
//
//

    @Override
    public void onResume() {
        super.onResume();
        if (!UtilString.isEmpty(BaseApplication.name)) {
            tv_name.setText(BaseApplication.name);
        }
        if (!UtilString.isEmpty(BaseApplication.avatar)) {
            ImageLoaderHelper.getInstance().setCirclePicture(mContext, img_head, BaseApplication.avatar);
        } else {
            if ("女".equals(BaseApplication.sex)) {
                img_head.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.icon_girl));
            } else {
                img_head.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.icon_boy));
            }
        }
        getOrderNumbers();

    }

    private void getUserInfo() {
        RestProxy.getInstance().getUserInfo(new BaseObserver<UserResponse>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onSuccess(UserResponse result) {
            }
        });
    }

    @OnClick({R.id.top, R.id.btn_daizhfu2, R.id.btn_daifahuo, R.id.btn_yifahuo, R.id.btn_yishouhuo, R.id.rl_prescription, R.id.my_medicine, R.id.btn_daily_record, R.id.btn_family, R.id.rl_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top:
                //用户中心
                startActivity(new Intent(mContext, UserInfoActivity.class));
                break;
            case R.id.btn_daizhfu2:
                //购买药品待支付
                Bundle bundle = new Bundle();
                bundle.putInt("page", 1);
                startActivity(MyMedicineActivity.class, bundle);
                break;
            case R.id.btn_daifahuo:
                Bundle bundleOne = new Bundle();
                bundleOne.putInt("page", 2);
                startActivity(MyMedicineActivity.class, bundleOne);
                break;
            case R.id.btn_yifahuo:
                Bundle bundleTwo = new Bundle();
                bundleTwo.putInt("page", 3);
                startActivity(MyMedicineActivity.class, bundleTwo);
                break;
            case R.id.btn_yishouhuo:
                Bundle bundleThree = new Bundle();
                bundleThree.putInt("page", 4);
                startActivity(MyMedicineActivity.class, bundleThree);
                break;
            case R.id.rl_prescription:
                startActivity(new Intent(mContext, MyMedicineActivity.class));
                break;
            case R.id.my_medicine:
                break;
            case R.id.btn_daily_record:
                //门诊挂号记录
                startActivity(DailyRecordListActivity.class);
                break;
            case R.id.btn_family:
                //家族成员
                Bundle bundleFamily = new Bundle();
                bundleFamily.putInt(UIConstants.INTO_MyFamilyActivity_FLAG, UIConstants.INTO_MyFamilyActivity_FLAG_1);
                startActivity(MyFamilyActivity.class, bundleFamily);
                break;
            case R.id.rl_setting:
                //帮助中心
                startActivity(SettingActivity.class);
                break;
            default:
                break;
        }
    }

    static class FragmentAdapter extends FragmentPagerAdapter {

        private static final int PAGE_COUNT = 2;

        FragmentAdapter(Context context, FragmentManager fm) {
            super(fm);
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return MineTuWenFragment.newInstance();
            }
            return MineCloudFragment.newInstance();
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "图文咨询";
            }
            return "云门诊";
        }

    }


    private void getOrderNumbers() {
        RestProxy.getInstance().getOrderNumbers(BaseApplication.currentUserId, new BaseObserver<List<OrderNumbersResponse>>() {
            @Override
            public void onSuccess(List<OrderNumbersResponse> orderNumbersResponse) {
                for (int i = 0; i < orderNumbersResponse.size(); i++) {
                    if (orderNumbersResponse.get(i).orderType == 4) {
                        for (int j = 0; j < orderNumbersResponse.get(i).orderNumbsers.size(); j++) {
                            if (orderNumbersResponse.get(i).orderNumbsers.get(j).orderStatus == 401)
                                setNum(tvUnreadCountDzf, orderNumbersResponse.get(i).orderNumbsers.get(j).number);
                            if (orderNumbersResponse.get(i).orderNumbsers.get(j).orderStatus == 402)
                                setNum(tvUnreadCountDfh, orderNumbersResponse.get(i).orderNumbsers.get(j).number);
                            if (orderNumbersResponse.get(i).orderNumbsers.get(j).orderStatus == 403)
                                setNum(tvUnreadCountDsh, orderNumbersResponse.get(i).orderNumbsers.get(j).number);
//                            if (orderNumbersResponse.data.get(i).orderNumbsers.get(j).orderStatus == 404)
//                                setNum(tvUnreadCountYsh, orderNumbersResponse.data.get(i).orderNumbsers.get(j).number);
                        }
                    }
                }
            }
        });
    }


    @SuppressLint("SetTextI18n")
    private void setNum(TextView tv, int num) {
        if (num == 0) {
            tv.setVisibility(View.INVISIBLE);
        } else {
            tv.setVisibility(View.VISIBLE);
            tv.setText(num + "");
        }
    }
}
